package com.duomn.mybatis.plugins.parse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Update implements IShardingParser {
	
	private static final String DELIMIT = " \t\n\r\f,";

	@Override
	public Map<String, Map<String, Object>> parse2Condition(String sql,
			List<Object> params, String tableName) {
		String lowSql = sql.toLowerCase();
		String lowTableName = sql.toLowerCase();
		if (lowSql.indexOf(lowTableName) == -1) { // 该sql不需要走分表
			return null;
		}
		
		/*
		 * update orders set name = 'asdfa', id = 'asd' where order_no = 'asdfa' and name = 'afha';
		 */
		int idx_tab_start = 7;  // update 的下标
		int idx_tab_end = lowSql.indexOf("set", 7);
		String tabsStr = lowSql.substring(idx_tab_start, idx_tab_end).trim();
		String[] tabs = tabsStr.split(",");
		Map<String, String> aliasMap = new HashMap<String, String>();
		for (String tab : tabs) {
			String[] tabNames = tab.trim().split("[ \t\n\r\f]+");
			if (tabNames.length == 3) { // 带别名  as 
				aliasMap.put(tabNames[2], tabNames[0]);
			} else if (tabNames.length == 2) { // 带别名 无as
				aliasMap.put(tabNames[1], tabNames[0]);
			}
		}
		
		int idx_cond_start = lowSql.indexOf("where", idx_tab_end);
		if (idx_cond_start == -1) {  // 无条件
			return null;
		}
		String whereStrs = lowSql.substring(idx_cond_start + 6).trim();
		String[] conditions = whereStrs.split("and");
		if (aliasMap.isEmpty()) { // 没有别名
			for (String cond : conditions) {
				
			}
		}
		
		return null;
	}

}
